package 剑指offer;

/**
 * @Description:
 * @Author: Administrator
 * @Date: 2020-05-05
 */
public class 两个链表的第一个公共结点 {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode temp1 = pHead1;
        ListNode temp2 = pHead2;
        ListNode temp1Le = temp1;
        ListNode temp2Le = temp2;
        int count1 = 0;
        int count2 = 0;
        while (temp1Le != null) {
            count1++;
            temp1Le = temp1Le.next;
        }
        while (temp2Le != null) {
            count2++;
            temp2Le = temp2Le.next;
        }
        ListNode node1 = null;
        ListNode node2 = null;
        if (count1 > count2) {
            node1 = temp1;
            node2 = temp2;
        } else {
            node1 = temp2;
            node2 = temp1;
        }
        while (node2 != null) {
            ListNode temp3 = node1;
            while (temp3 != null) {
                if (temp3.val == node2.val) {
                    return node2;
                }
                temp3 = temp3.next;
            }
            node2 = node2.next;
        }
        return null;
    }
}
